﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EQ1.Elements;
using com.woodsoft.core;
using com.woodsoft.core.Files;
using System.Xml;
using EQ1.Objects;

namespace EQ1.Controls {
  public class Listbox : Control , IUIElement<Listbox> {
    public Listbox ( UIDocument doc , string name )
      : base( doc , name ) {
    }
    public Listbox ( UIDocument doc )
      : this( doc , "Listbox" ) {
    }

    #region IUIElement<Listbox> Members

    public void Validate ( Listbox val , string name ) {
      UIClass par = ( UIClass ) this.ParentNode;

      if( val.LocalName != name ) {
        Listbox el = new Listbox( ( UIDocument ) val.OwnerDocument , name );
        el.Copy( val );
        val = el;
      }

      if( par.GetElement( name ) != null )
        par.RemoveElement( name );

      par.AppendChild( val );
    }
    public void Copy ( Listbox val ) {
      base.Copy( val );

      if( val.Columns != null )
        this.Columns = val.Columns;
      if( val.OwnerDraw != null )
        this.OwnerDraw = val.OwnerDraw;
    }

    #endregion

    public XmlNodeList Columns {
      get {
        return this.GetElementsByTagName( "Columns" );
      }
      set {
        foreach( ListboxColumn nd in value ) {
          this.AddColumn( nd );
        }
      }
    }
    public bool? OwnerDraw {
      get {
        return bool.Parse( this.GetElement( "OwnerDraw" ).InnerText );
      }
      set {
        this.Validate( "OwnerDraw" , value.ToString() );
      }
    }

    public ListboxColumn GetColumn ( string item ) {
      foreach( ListboxColumn nd in this.Columns ) {
        if( nd.item == item ) {
          return nd;
        }
      }

      return null;
    }
    public void AddColumn ( ListboxColumn val ) {
      for( int i = this.Columns.Count - 1 ; i >= 0 ; i-- ) {
        if( ( ( ListboxColumn ) this.Columns[i] ).item == val.item ) {
          this.ReplaceChild( val , this.Columns[i] );
          return;
        }
      }

      this.AppendChild( val );
    }
  }
}
